/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.net.URI;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpState;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.params.HttpClientParams;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestCollectConfigServletsAccess
/*     */   extends TestCase
/*     */ {
/*  37 */   private static String TEST_ADMIN_NAME = "TestAdmin";
/*  38 */   private static String PASSWORD = "test123";
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  42 */     cleanUp();
/*     */     
/*  44 */     Map<String, Object> attrs = new HashMap();
/*  45 */     attrs.put("zimbraIsDelegatedAdminAccount", "TRUE");
/*  46 */     attrs.put("displayName", "delegated admin for a unit tests");
/*  47 */     Provisioning.getInstance().createAccount(TestUtil.getAddress(TEST_ADMIN_NAME), PASSWORD, attrs);
/*  48 */     Provisioning.getInstance().grantRight(TargetType.domain.getCode(), TargetBy.name, TestUtil.getDomain(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, TestUtil.getAddress(TEST_ADMIN_NAME), null, "domainAdminConsoleRights", null);
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/*  54 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception {
/*  58 */     Provisioning prov = Provisioning.getInstance();
/*  59 */     Account account = prov.get(Key.AccountBy.name, TestUtil.getAddress(TEST_ADMIN_NAME));
/*  60 */     if (account != null) {
/*  61 */       prov.deleteAccount(account.getId());
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void testConfigDelegatedAdmin()
/*     */     throws Exception
/*     */   {
/*  93 */     ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
/*  94 */     URI servletURI = new URI(getConfigServletUrl());
/*  95 */     HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
/*  96 */     HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*  97 */     restClient.setState(initialState);
/*  98 */     restClient.getParams().setCookiePolicy("compatibility");
/*  99 */     GetMethod get = new GetMethod(servletURI.toString());
/* 100 */     int statusCode = HttpClientUtil.executeMethod(restClient, get);
/* 101 */     assertEquals("This request should NOT succeed. Getting status code " + statusCode, 401, statusCode);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void testLDAPConfigDelegatedAdmin()
/*     */     throws Exception
/*     */   {
/* 131 */     ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
/* 132 */     URI servletURI = new URI(getLDAPConfigServletUrl());
/* 133 */     HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
/* 134 */     HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 135 */     restClient.setState(initialState);
/* 136 */     restClient.getParams().setCookiePolicy("compatibility");
/* 137 */     GetMethod get = new GetMethod(servletURI.toString());
/* 138 */     int statusCode = HttpClientUtil.executeMethod(restClient, get);
/* 139 */     assertEquals("This request should NOT succeed. Getting status code " + statusCode, 401, statusCode);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void testLDAPConfigNoToken()
/*     */     throws Exception
/*     */   {
/* 148 */     URI servletURI = new URI(getLDAPConfigServletUrl());
/* 149 */     HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 150 */     restClient.getParams().setCookiePolicy("compatibility");
/* 151 */     GetMethod get = new GetMethod(servletURI.toString());
/* 152 */     int statusCode = HttpClientUtil.executeMethod(restClient, get);
/* 153 */     assertEquals("This request should NOT succeed. Getting status code " + statusCode, 401, statusCode);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Test
/*     */   public void testConfigNoToken()
/*     */     throws Exception
/*     */   {
/* 162 */     URI servletURI = new URI(getConfigServletUrl());
/* 163 */     HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 164 */     restClient.getParams().setCookiePolicy("compatibility");
/* 165 */     GetMethod get = new GetMethod(servletURI.toString());
/* 166 */     int statusCode = HttpClientUtil.executeMethod(restClient, get);
/* 167 */     assertEquals("This request should NOT succeed. Getting status code " + statusCode, 401, statusCode);
/*     */   }
/*     */   
/*     */   private static String getConfigServletUrl() throws ServiceException {
/*     */     int port;
/*     */     try {
/* 173 */       port = Provisioning.getInstance().getLocalServer().getIntAttr("zimbraAdminPort", 0);
/*     */     } catch (ServiceException e) {
/* 175 */       ZimbraLog.test.error("Unable to get admin SOAP port", e);
/* 176 */       port = LC.zimbra_admin_service_port.intValue();
/*     */     }
/* 178 */     return "https://localhost:" + port + "/service/collectconfig/?host=" + TestUtil.getServerAttr("name");
/*     */   }
/*     */   
/*     */   private static String getLDAPConfigServletUrl() throws ServiceException {
/*     */     int port;
/*     */     try {
/* 184 */       port = Provisioning.getInstance().getLocalServer().getIntAttr("zimbraAdminPort", 0);
/*     */     } catch (ServiceException e) {
/* 186 */       ZimbraLog.test.error("Unable to get admin SOAP port", e);
/* 187 */       port = LC.zimbra_admin_service_port.intValue();
/*     */     }
/* 189 */     return "https://localhost:" + port + "/service/collectldapconfig/";
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestCollectConfigServletsAccess.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */